package gov.dhs.cis.soa.readers;

import gov.dhs.cis.soa.SOAData;
import gov.dhs.cis.soa.Utils;
import gov.dhs.cis.soa.data.RelatedProperties;
import gov.dhs.cis.soa.data.Service;
import gov.dhs.cis.soa.data.ServiceOperation;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;


public class ServicesLayerReader extends ExcelReader {
	static final Logger loger = Logger.getLogger(ServicesLayerReader.class);

	private final String SRV_FILE_NAME = "USCIS Architecture Components - Service Layer.xls";
	private String SERV_SHEET_NAME = "Unique Services";
	private String SERV_OP_SHEET_NAME = "SOD";

	private String SERV_COL_NUM = "D";
	private String DESC_COL_NUM = "E";
	private String STEREO_COL_NUM = "G";
	private String WIPT_COL_NUM = "H";
	private String REL_COL_NUM = "I";
	private String MATURITY_COL_NUM = "J";
	private String APP_COL_NUM = "L";
	
	private String EXTSYS_COL_NUM = "M";
	private String OTHER_SERV_COL_NUM = "N";
	private String L3_BUS_PROC_COL_NUM = "R";
	
	private String SERV_DEF_COL_NUM = "S";
	private String AGNT_COL_NUM = "U";
	
	private String EXPOS_COL_NUM = "V";
	private String REALIZE_COL_NUM = "W";
	private String I_HOST_CFG_COL_NUM = "X";
	private String I_ESB_ENDPT_COL_NUM = "Y";
	
	private String DA_COL_NUM = "AB";
	private String COMP_COL_NUM = "AC";
	
	private String OP_SERV_COL_NUM = "D";
	private String OP_OPER_COL_NUM = "E";
	private String OP_DATA_IN_COL_NUM = "F";
	private String OP_DATA_OUT_COL_NUM = "G";

	private int MAX_SERVS = 156;
	private int MAX_SERVS_OP = 420;
	
	public SOAData extractServices(SOAData soaData) {
		loger.debug("Extracting Services (Definations) layer Sheet...");
		HSSFWorkbook w = openFile(SRV_FILE_NAME);
		HSSFSheet s1 = w.getSheet(SERV_SHEET_NAME);
		int rows = s1.getLastRowNum();
		loger.debug("S1 rows =" + rows);

		ArrayList<Service> services = new ArrayList<Service>();
		soaData.servList = services;
		for (int n = 2; n < MAX_SERVS; n++) {
			HSSFRow row = s1.getRow(n);
			if(row == null)
				continue;
			String data = getCellStringValue(w, row, SERV_COL_NUM);
			if(Utils.isEmpty(data)) {
				loger.warn("Service Name is Null/Empty at row : " + n + ", ignoring this row.");
				continue;
			}
			Service s = new Service();
			s.id = Utils.getNextId();
			s.name = getCellStringValue(w, row, SERV_COL_NUM);
			s.descText = getCellStringValue(w, row, DESC_COL_NUM);
			s.stereotype = getCellStringValue(w, row, STEREO_COL_NUM);
			if(Utils.isEmpty(s.stereotype))
				s.stereotype = Service.STEREO_TYPE;
			s.wipt = getCellStringValue(w, row, WIPT_COL_NUM);
			if(Utils.isEmpty(s.wipt))
				s.wipt = "-1";
			s.release = Utils.convert2Release(getCellStringValue(w, row, REL_COL_NUM));
			s.maturity = Utils.convert1stchar2Int(getCellStringValue(w, row, MATURITY_COL_NUM));
			s.applications = getCellStringValue(w, row, APP_COL_NUM);
			s.extsystems = getCellStringValue(w, row, EXTSYS_COL_NUM);
			s.otherservices = getCellStringValue(w, row, OTHER_SERV_COL_NUM);
			s.l3busprocname = getCellStringValue(w, row, L3_BUS_PROC_COL_NUM);
			s.exposure = getCellStringValue(w, row, EXPOS_COL_NUM);
			
			s.servDefID = getCellStringValue(w, row, SERV_DEF_COL_NUM);
			s.agent = getCellStringValue(w, row, AGNT_COL_NUM);
			
//			if("Internal".equalsIgnoreCase(s.exposure))
//				s.exposure = "I";
//			else
//				s.exposure = "E";
			s.realization = getCellStringValue(w, row, REALIZE_COL_NUM);
			s.inthostcfg = getCellStringValue(w, row, I_HOST_CFG_COL_NUM);
			s.intesbendpt = getCellStringValue(w, row, I_ESB_ENDPT_COL_NUM);
			
			s.designAspects = getCellStringValue(w, row, DA_COL_NUM);
			s.exposure = getCellStringValue(w, row, EXPOS_COL_NUM);
			
			String compID = getCellStringValue(w, row, COMP_COL_NUM);
			
			if(soaData.propsList == null)
				soaData.propsList = new ArrayList<RelatedProperties>();
			soaData.propsList.addAll(RelatedProperties.getNewServiceProperty(s, "COMP_ID", compID));
			//soaData.propsList.addAll(RelatedProperties.getNewServiceProperty(s.id, "Realization", realize));

			s.createDescription();
			services.add(s);
		}
		closeAll(w);
		return soaData;
	}
	
	public SOAData extractServiceOperations(SOAData soaData) {
		loger.debug("Extracting Services (Operations) layer Sheet...");
		HSSFWorkbook w = openFile(SRV_FILE_NAME);
		HSSFSheet s1 = w.getSheet(SERV_OP_SHEET_NAME);
		int rows = s1.getLastRowNum();
		loger.debug("S1 rows =" + rows);

		List<Service> services = soaData.servList;
		soaData.servList = services;
		for (int n = 2; n < MAX_SERVS_OP; n++) {
			HSSFRow row = s1.getRow(n);
			if(row == null)
				continue;

			String servName = getCellStringValue(w, row, OP_SERV_COL_NUM);
			if(Utils.isEmpty(servName)) {
				loger.warn("Service Name is Null/Empty at row : " + n + ", ignoring this row.");
				continue;
			}
			Service s = Utils.findServiceByName(servName, services);
			if(s == null) {
				loger.error("Couldnt locate Service <" + servName + "> at row : " + n + ", ignoring this row.");
				continue;
			}
			String servOpName = getCellStringValue(w, row, OP_OPER_COL_NUM);
			if(Utils.isEmpty(servOpName)) {
				loger.warn(servName + ": Operation Name is Null/Empty at row : " + n + ", ignoring this row.");
				continue;
			}
			ServiceOperation sOp = new ServiceOperation();
			sOp.name = servOpName;
			sOp.id = Utils.getNextId();
			sOp.dataIn = getCellStringValue(w, row, OP_DATA_IN_COL_NUM);
			sOp.dataOut = getCellStringValue(w, row, OP_DATA_OUT_COL_NUM);
			s.add(sOp);
		}
		closeAll(w);
		return soaData;
	}
}
